package com.kingdee.eas.xr.helper;

/*jadclipse*/// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.


import com.kingdee.eas.fm.common.FMHelper;
import com.kingdee.util.DateTimeUtils;
import java.io.PrintStream;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import org.apache.log4j.Logger;

public class ECDateHelper
{

    public ECDateHelper()
    {
    }

    public static List getTimeList(int beginYear, int beginMonth, int endYear, int endMonth, int k)
    {
        List list = new ArrayList();
        if(beginYear == endYear)
        {
            for(int j = beginMonth; j <= endMonth; j++)
                list.add(getTimeList(beginYear, j, k));

        } else
        {
            for(int j = beginMonth; j < 12; j++)
                list.add(getTimeList(beginYear, j, k));

            for(int i = beginYear + 1; i < endYear; i++)
            {
                for(int j = 0; j < 12; j++)
                    list.add(getTimeList(i, j, k));

            }

            for(int j = 0; j <= endMonth; j++)
                list.add(getTimeList(endYear, j, k));

        }
        return list;
    }

    public static List getTimeList(int beginYear, int beginMonth, int k)
    {
        List list = new ArrayList();
        Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
        int max = begincal.getActualMaximum(5);
        for(int i = 1; i < max; i += k)
        {
            list.add(begincal.getTime());
            begincal.add(5, k);
        }

        begincal = new GregorianCalendar(beginYear, beginMonth, max);
        list.add(begincal.getTime());
        return list;
    }

    public static java.util.Date getDayBegin()
    {
        Calendar cal = new GregorianCalendar();
        cal.set(11, 0);
        cal.set(12, 0);
        cal.set(13, 0);
        cal.set(14, 0);
        return cal.getTime();
    }

    public static Timestamp getTimeStampDayBegin()
    {
        return new Timestamp(getDayBegin().getTime());
    }

    public static java.util.Date getDayEnd()
    {
        Calendar cal = new GregorianCalendar();
        cal.set(11, 23);
        cal.set(12, 59);
        cal.set(13, 59);
        return cal.getTime();
    }

    public static java.util.Date getNextDay(java.util.Date date)
    {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(5, cal.get(5) + 1);
        return cal.getTime();
    }

    public static java.util.Date getNextMonth(java.util.Date date)
    {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(2, cal.get(2) + 1);
        return cal.getTime();
    }

    public static java.util.Date getPreMonth(java.util.Date date)
    {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(2, cal.get(2) - 1);
        return cal.getTime();
    }

    public static java.util.Date getPreMonthMaxDate(java.util.Date date)
    {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(5, 1);
        cal.set(2, cal.get(2) - 1);
        cal.set(5, cal.getActualMaximum(5));
        return DateTimeUtils.truncateDate(cal.getTime());
    }

    public static java.util.Date getNextMonthMaxDate(java.util.Date date)
    {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(5, 1);
        cal.set(2, cal.get(2) + 1);
        cal.set(5, cal.getActualMaximum(5));
        return DateTimeUtils.truncateDate(cal.getTime());
    }

    public static Date getSQLBegin(java.util.Date da)
    {
        Calendar cal = new GregorianCalendar();
        cal.setTime(da);
        cal.set(11, 0);
        cal.set(12, 0);
        cal.set(13, 0);
        cal.set(14, 0);
        Date ds = new Date(cal.getTimeInMillis());
        return ds;
    }

    public static Date getSQLEnd(java.util.Date da)
    {
        Calendar cal = new GregorianCalendar();
        cal.setTime(da);
        cal.set(11, 23);
        cal.set(12, 59);
        cal.set(13, 59);
        return new Date(cal.getTimeInMillis());
    }

    public static java.util.Date getFirstSeasonDate(java.util.Date date)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int sean = getSeason(cal.get(2));
        cal.set(2, sean * 3 - 2);
        return cal.getTime();
    }

    public static java.util.Date getFirstYearDate(java.util.Date date)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(2, 0);
        cal.set(5, 1);
        return DateTimeUtils.truncateDate(cal.getTime());
    }

    public static java.util.Date getLastYearDate(java.util.Date date)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(2, 11);
        cal.set(5, 31);
        return DateTimeUtils.truncateDate(cal.getTime());
    }

    public static int getSeason(int mouth)
    {
        return SEASON[mouth];
    }

    public static java.util.Date getBeforeDay(java.util.Date thisDate)
    {
        Calendar cal = new GregorianCalendar();
        cal.setTime(thisDate);
        cal.add(5, -1);
        return cal.getTime();
    }

    public static java.util.Date getAfterDay(java.util.Date thisDate)
    {
        Calendar cal = new GregorianCalendar();
        cal.setTime(thisDate);
        cal.add(5, 1);
        return cal.getTime();
    }

    public static boolean isFirstDayOfYear(java.util.Date date)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(2) == 0 && cal.get(5) == 1;
    }

    public static java.util.Date getFirstYearDate(int year)
    {
        Calendar cal = Calendar.getInstance();
        cal.set(1, year);
        cal.set(2, 0);
        cal.set(5, 1);
        return DateTimeUtils.truncateDate(cal.getTime());
    }

    public static java.util.Date stringToDate(String s)
    {
        java.util.Date d = null;
        if(FMHelper.isEmpty(s))
            d = getDayBegin();
        else
            try
            {
                d = FORMAT_DAY.parse(s);
            }
            catch(ParseException e)
            {
                try
                {
                    d = (new SimpleDateFormat()).parse(s);
                }
                catch(ParseException e1)
                {
                    d = getDayBegin();
                }
            }
        return d;
    }

    public static String DateToString(java.util.Date s)
    {
        return FORMAT_DAY.format(s);
    }

    public static java.util.Date getLastYearDate(int year)
    {
        Calendar cal = Calendar.getInstance();
        cal.set(1, year);
        cal.set(2, 11);
        cal.set(5, 31);
        return DateTimeUtils.truncateDate(cal.getTime());
    }

    public static long dateDiff(java.util.Date date1, java.util.Date date2)
    {
        long date1ms = date1.getTime();
        long date2ms = date2.getTime();
        return date2ms - date1ms;
    }

    public static java.util.Date getDayBegin(java.util.Date da)
    {
        Calendar cal = new GregorianCalendar();
        cal.setTime(da);
        cal.set(11, 0);
        cal.set(12, 0);
        cal.set(13, 0);
        cal.set(14, 0);
        java.util.Date ds = new java.util.Date(cal.getTimeInMillis());
        return ds;
    }

    public static long dateDiff(String interval, java.util.Date dDate1, java.util.Date dDate2)
    {
        int desiredField = 0;
        int coef = 1;
        java.util.Date date1;
        java.util.Date date2;
        if(dDate1.getTime() > dDate2.getTime())
        {
            coef = -1;
            date1 = dDate2;
            date2 = dDate1;
        } else
        {
            date1 = dDate1;
            date2 = dDate2;
        }
        int field;
        if(interval.equals("yyyy"))
            field = 1;
        else
        if(interval.equals("m"))
            field = 2;
        else
        if(interval.equals("d"))
            field = 5;
        else
        if(interval.equals("y"))
            field = 5;
        else
        if(interval.equals("w"))
            field = 4;
        else
        if(interval.equals("ww"))
            field = 3;
        else
        if(interval.equals("h"))
        {
            field = 5;
            desiredField = 11;
        } else
        if(interval.equals("n"))
        {
            field = 5;
            desiredField = 12;
        } else
        if(interval.equals("s"))
        {
            field = 5;
            desiredField = 13;
        } else
        {
            return -1L;
        }
        Calendar calTmp = Calendar.getInstance();
        calTmp.setTime(date1);
        long nbOccurence = 0L;
        calTmp.add(field, 1);
        for(java.util.Date dateTemp = calTmp.getTime(); dateTemp.getTime() <= date2.getTime();)
        {
            calTmp.add(field, 1);
            dateTemp = calTmp.getTime();
            nbOccurence++;
        }

        if(desiredField == 11 || desiredField == 12 || desiredField == 13)
        {
            calTmp.setTime(date1);
            calTmp.add(field, (int)nbOccurence);
            java.util.Date dateTemp = calTmp.getTime();
            switch(desiredField)
            {
            case 11: // '\013'
                nbOccurence *= 24L;
                break;

            case 12: // '\f'
                nbOccurence = nbOccurence * 24L * 60L;
                break;

            case 13: // '\r'
                nbOccurence = nbOccurence * 24L * 60L * 60L;
                break;
            }
            calTmp.add(desiredField, 1);
            for(dateTemp = calTmp.getTime(); dateTemp.getTime() <= date2.getTime();)
            {
                calTmp.add(desiredField, 1);
                dateTemp = calTmp.getTime();
                nbOccurence++;
            }

        }
        return nbOccurence * (long)coef;
    }

    public static java.util.Date getFirstDayOfCurMonth()
    {
        Calendar cal = Calendar.getInstance();
        cal.set(5, cal.getActualMinimum(5));
        return DateTimeUtils.truncateDate(cal.getTime());
    }

    public static java.util.Date getFirstDayOfMonth(java.util.Date d)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.set(5, cal.getActualMinimum(5));
        return DateTimeUtils.truncateDate(cal.getTime());
    }

    public static java.util.Date getEndDayOfCurMonth()
    {
        Calendar cal = Calendar.getInstance();
        cal.set(5, cal.getActualMaximum(5));
        return DateTimeUtils.truncateDate(cal.getTime());
    }

    public static java.util.Date getLastDayOfCurMonth()
    {
        Calendar cal = Calendar.getInstance();
        cal.set(5, cal.getActualMaximum(5));
        return DateTimeUtils.truncateDate(cal.getTime());
    }

    public static java.util.Date getLastDayOfMonth(java.util.Date date)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(2, 1);
        calendar.set(5, 1);
        calendar.add(5, -1);
        calendar.set(11, calendar.getActualMaximum(11));
        calendar.set(12, calendar.getActualMaximum(12));
        calendar.set(13, calendar.getActualMaximum(13));
        return calendar.getTime();
    }

    public static java.util.Date getLastDayOfMonth2(java.util.Date date)
    {
        return DateTimeUtils.truncateDate(getLastDayOfMonth(date));
    }

    public static String formatDate(java.util.Date d)
    {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        return df.format(d);
    }

    public static String formatDate2(java.util.Date d)
    {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(d);
    }

    public static java.util.Date max(java.util.Date date1, java.util.Date date2)
    {
        if(date1 == null)
            return date2;
        if(date2 == null)
            return date1;
        if(date1.after(date2))
            return date1;
        else
            return date2;
    }

    public static java.util.Date min(java.util.Date date1, java.util.Date date2)
    {
        if(date1 == null)
            return date2;
        if(date2 == null)
            return date1;
        if(date1.after(date2))
            return date2;
        else
            return date1;
    }

    public static Date truncateSqlDate(Date sqlDate)
    {
        java.util.Date date = new java.util.Date(sqlDate.getTime());
        return new Date(DateTimeUtils.truncateDate(date).getTime());
    }

    public static int getDiffDays(java.util.Date beginDate, java.util.Date endDate)
    {
        if(beginDate == null || endDate == null)
        {
            throw new IllegalArgumentException("getDiffDays param is null!");
        } else
        {
            long diff = (endDate.getTime() - beginDate.getTime()) / 86400000L;
            int days = (new Long(diff)).intValue() + 1;
            return days;
        }
    }

    public static Date getSqlDate(java.util.Date date)
    {
        Date sqlDate = new Date(date.getTime());
        return sqlDate;
    }

    /**
     * @deprecated Method getYearDate is deprecated
     */

    public static java.util.Date getYearDate(java.util.Date date)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(cal.get(1) + 1, 0, 0, 0, 0, 0);
        return new java.util.Date((cal.getTimeInMillis() / 1000L) * 1000L);
    }

    public static java.util.Date getMonthDate(java.util.Date date)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(cal.get(1), cal.get(2) + 1, 0, 0, 0, 0);
        return new java.util.Date((cal.getTimeInMillis() / 1000L) * 1000L);
    }

    public static java.util.Date getDayDate(java.util.Date date)
    {
        return DateTimeUtils.truncateDate(date);
    }

    public static java.util.Date addDays(java.util.Date date, int day)
    {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(5, day);
        return calendar.getTime();
    }

    public static void main(String args[])
    {
        java.util.Date date = new java.util.Date();
        Calendar cal = Calendar.getInstance();
        cal.set(2, 9);
        cal.set(5, 31);
        System.out.println((new StringBuilder()).append("date next:").append(formatDate2(cal.getTime())).toString());
        date = getNextMonthMaxDate(cal.getTime());
        System.out.println((new StringBuilder()).append("date2:").append(formatDate2(date)).toString());
        date = getPreMonthMaxDate(cal.getTime());
        System.out.println((new StringBuilder()).append("date pre:").append(formatDate2(date)).toString());
    }

    Logger logger;
    public static final int SEASON[] = {
        1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 
        4, 4
    };
    public static final DateFormat FORMAT_DAY = new SimpleDateFormat("yyyy-MM-dd");

}


/*
	DECOMPILATION REPORT

	Decompiled from: F:\qs\lib\industry\ec_basedata-server.jar
	Total time: 31 ms
	Jad reported messages/errors:
	Exit status: 0
	Caught exceptions:
*/